import { compileToFunctions } from "./compiler/index";
import { initState } from "./initState";
import { mountComponent } from "./lifecycle";

export default function initMixin(Vue) {
    Vue.prototype._init = function(options) {
       const vm = this;
       this.$options = options;
       initState(vm);
    //    挂载
       if(vm.$options.el) {
        vm.$mount(vm.$options.el);
       }
    }

    Vue.prototype.$mount = function(el) {
        const vm = this;
        el = document.querySelector(el); // el 就是 #app
        const options = vm.$options;
        if(!options.render) {
            let template = options.template;
            if(!template) {
                template = el.outerHTML;
            }
            const render = compileToFunctions(template);
            options.render = render;
        }
        mountComponent(vm, el);
    }
}